﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Common.Script;

public partial class Theme_DL_MyClassTasks : PowerCreator.LMS.Web.Base.WebBasePage
{
    protected string ClassName = string.Empty;
    protected string CourseNum = string.Empty;
    private string CourseID = string.Empty;
    PowerCreator.LMS.ViewQuery.ViewQuery obj = new PowerCreator.LMS.ViewQuery.ViewQuery();
    VOTOClassTasks voClassTask = new VOTOClassTasks();
    TOClassTasks objClassTask = new TOClassTasks();
    private VOTOClass voClass = new VOTOClass();
    TOClass objClass = new TOClass();
    VOTOClassTasksReply votaskreply = new VOTOClassTasksReply();
    TOClassTasksReply objtaskReply = new TOClassTasksReply();
    PowerCreator.LMS.ViewQuery.ViewQuery ov = new PowerCreator.LMS.ViewQuery.ViewQuery();

    protected void Page_Load(object sender, EventArgs e)
    {
        int CID = 0;
        if (Request.QueryString["ClassID"] != null)
        {
            CID = int.Parse(Request.QueryString["ClassID"].ToString());
            ViewState["ClassID"] = CID.ToString();
            voClass = objClass.Load(CID);
            ClassName = voClass.Name;
            BinderPG(voClass);
        }
    }

    private void BinderPG(VOTOClass vo)
    {
        VOTOCourseList voCourselist = new VOTOCourseList();
        TOCourseList objCourselist = new TOCourseList();
        TOUserClassTask objuserclasstask = new TOUserClassTask();
        PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login != null)
        {
            voCourselist = objCourselist.Load(vo.ClassCourseListID);
            string CourseIDsList = voCourselist.CourseID;
            string[] courseid = CourseIDsList.Split('#');
            CourseNum = courseid.Length.ToString();
            string courseidlist = string.Empty;
            for (int i = 0; i < courseid.Length; i++)
            {
                string[] par = courseid[i].Split(',');
                if (par.Length == 4)
                {
                    if (courseidlist.Length > 0)
                        courseidlist += ",";
                    courseidlist += par[0];
                }
            }
            if (courseidlist.Length == 0)
                courseidlist = "0";

        string search = "SELECT TO_Course.Course_Id, TO_Course.CourseName, TO_ClassTasks.ClassTasks_Id, TO_ClassTasks.ClassID, TO_ClassTasks.TasksTitle, TO_ClassTasks.EndDate, TO_ClassTasks.Broswer,TO_ClassTasks.CreatDate, TO_ClassTasks.SetTop, TO_ClassTasks.SetHighLight FROM TO_ClassTasks,TO_Course where TO_ClassTasks.Course_ID = TO_Course.Course_Id  and TO_ClassTasks.ClassID=" + ViewState["ClassID"].ToString();
        DataTable dt = obj.Query(search);

        search = "SELECT  Course_ID,ClassTasks_Id, ClassID, TasksTitle, EndDate, Broswer,CreatDate, SetTop,SetHighLight FROM TO_ClassTasks where Course_ID = 0  and ClassID=" + ViewState["ClassID"].ToString();
        DataTable dt03 = obj.Query(search);
        dt03.Columns.Add("CourseName");

        for (int k = 0; k < dt03.Rows.Count; k++)
        {
            dt03.Rows[k]["CourseName"] = "--";

            dt.Rows.Add(dt03.Rows[k]["Course_Id"], dt03.Rows[k]["CourseName"], dt03.Rows[k]["ClassTasks_Id"], dt03.Rows[k]["ClassID"], dt03.Rows[k]["TasksTitle"], dt03.Rows[k]["EndDate"], dt03.Rows[k]["Broswer"], dt03.Rows[k]["CreatDate"], dt03.Rows[k]["SetTop"], dt03.Rows[k]["SetHighLight"], dt03.Rows[k]["rowstat"]);


        }


        dt.Columns.Add("IsFinish");
        dt.Columns.Add("UserTask_ID");
        dt.Columns.Add("Score");

        for (int p = 0; p < dt.Rows.Count; p++)
        {
            DataTable dtusertask = new DataTable();
            dtusertask = objuserclasstask.Query("Task_Id=" + dt.Rows[p]["ClassTasks_Id"].ToString() + " and Creator=" + login.UserID.ToString() + "");
            if (dtusertask.Rows.Count > 0)
            {
                dt.Rows[p]["UserTask_ID"] = dtusertask.Rows[0]["UserTask_ID"].ToString();
                dt.Rows[p]["Score"] = getPerScore(dtusertask.Rows[0]["UserTask_ID"].ToString());
                dt.Rows[p]["IsFinish"] = "已完成";
            }
            else
            {
                dt.Rows[p]["UserTask_ID"] = "0";
                dt.Rows[p]["Score"] = "--";
                dt.Rows[p]["IsFinish"] = "未完成";
            }

        }


        rptNew.DataSource = dt;
        rptNew.DataBind();

        }
    }
    private string getPerScore(string UserTask_ID)
    {
        string sql = "";
        int total = 0;
        int num = 0;
        string per = "--";
        DataTable dt = new DataTable();

        sql = " ReplyType=1 and UserTask_Id=" + UserTask_ID;
        num = objtaskReply.Count(sql);
        if (num > 0)
        {
            sql = "select sum(Point) as sum from TO_ClassTasksReply where "+sql;
            dt = ov.Query(sql);
            if (dt.Rows.Count == 1)
            {
                total = int.Parse(dt.Rows[0]["sum"].ToString());
                per = (total / num).ToString();
            }
        }

        return per;

    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lnkbtn_editor = (LinkButton)e.Item.FindControl("lnkbtn_editor");
            LinkButton lnkbtn_look = (LinkButton)e.Item.FindControl("lnkbtn_look");
            Label lab_ID = (Label)e.Item.FindControl("lab_ID");
            Label lbl_UserTask_ID = (Label)e.Item.FindControl("lbl_UserTask_ID");

            if (lab_ID.Text == "0")
            {
                lnkbtn_editor.Enabled = false;
            }
            if (lbl_UserTask_ID.Text == "0")
            {
                lnkbtn_look.Enabled = false;
            }
        }
    }




    protected void rptNew_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        VOTOClassTasks vo = new VOTOClassTasks();
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");
        Label lbl_CourseID = (Label)e.Item.FindControl("lbl_CourseID");
        Label lbl_UserTask_ID = (Label)e.Item.FindControl("lbl_UserTask_ID");

        if (e.CommandName.ToLower() == "leditor")
        {
            string url = "DealClassTask.aspx?ClassID=" + ViewState["ClassID"].ToString() + "&CourseID=" + lbl_CourseID.Text + "&TaskID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);

        }
        else if (e.CommandName.ToLower() == "llook")
        {
            string url = "UserClassTasks.aspx?UserTask_ID=" + lbl_UserTask_ID.Text + "&ClassID=" + ViewState["ClassID"].ToString() + "&CourseID=" + lbl_CourseID.Text + "&TaskID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);

        }

       

    }
   

}
